<template>
  <div class="detail">
    <el-card>
      <div class="btnbox">
        <el-button @click="toprint" class="btn" size="mini" type="primary"
          >打印</el-button
        >
      </div>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录帐户设置" name="first">
          <el-form :model="userInfo" class="myform" label-width="100px">
            <el-form-item label="姓名">
              <el-input v-model="userInfo.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="userInfo.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="update" type="primary">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <userinfo></userinfo>
        </el-tab-pane>
        <el-tab-pane label="岗位信息" name="third">
          <jobinfo></jobinfo>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
// 导入 api
import { getUserInfoApi, setUserInfoApi } from '@/api/employees.js'
import userinfo from '../components/user-info.vue'
import jobinfo from '../components/job-info.vue'
export default {
  data () {
    return {
      activeName: 'first',
      // 得到用户 id
      id: this.$route.query.id,
      // 表单的数据源
      userInfo: {
        username: '',
        password: ''
      },
      // 保存其它信息
      info: {}
    }
  },
  methods: {
    // 跳转到打印页面
    toprint () {
      this.$router.push(`/employees/print/${this.id}?type=${this.activeName}`)
    },
    // 得到用户信息
    async getUserInfo () {
      const res = await getUserInfoApi(this.id)
      this.info = res
      // console.log(res)
      // 我们只渲染用户名，密码不管
      this.userInfo.username = res.username
    },
    // 更新数据
    async update () {
      try {
        await setUserInfoApi({
          ...this.info,
          ...this.userInfo
        })
        this.$message.success('更新成功')
        // 跳转回员工页面
        this.$router.push('/employees')
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  created () {
    this.getUserInfo()
  },
  components: {
    userinfo,
    jobinfo
  }
}
</script>

<style lang="scss" scoped>
.detail {
  padding: 20px;
  .myform {
    width: 400px;
    margin-left: 200px;
  }
  .btnbox {
    position: relative;
    .btn {
      position: absolute;
      right: 10px;
      top: 0px;
      z-index: 999;
    }
  }
}
</style>
